package com.ceba.test.modules.regist.dao;

import abc.ID;
import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.data.Sql;
import abc.object.XObject;
import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.modules.regist.entity.NewLicLog;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.List;

public class NewLicLogDao {

    public Boolean add(NewLicLog newLicLog) throws Exception{
        IStore store = StoreManager.create("g_lic_log");
        IObject object = new XObject("g_lic_log");
        object.put("ID", ID.getGuid());
        object.put("enterpriseId",newLicLog.getEnterpriseId());
        object.put("AVAILABLE_DATE", newLicLog.getAvailbaleDate());
        object.put("ALL_SIGN_NUM",newLicLog.getAllSignNum());
        object.put("SUBORDINATE_SERVICE",newLicLog.getSubordianteService());
        String keyMode = newLicLog.getKeyMode();
        if(keyMode.equals("0")){
            keyMode = ResourceUtils.getMessage("REGISTER_CLIENT_MODE");
        }else{
            keyMode = ResourceUtils.getMessage("REGISTER_SERVER_SIDE_MODE");
        }
        object.put("KEY_MODE",newLicLog.getKeyMode());
        object.put("CREATE_PDF_MODE",newLicLog.getCreatePdfMode());
        object.put("CERTIFICATE_EXPIRY_TIME",newLicLog.getCertificateExpiryTime());
        object.setCreateTime();
        if(store.insert(object)>0){
            return true;
        }
        return false;
    }

    public List<NewLicLog> queryLicLogAll(String enterpriseId){
        List<NewLicLog> list = new ArrayList<NewLicLog>();
        Sql sql = new Sql("select log.*,en.ENTERPRISE_NAME  from g_lic_log log LEFT JOIN lic_enterprise en ON enterpriseId=en.Id where enterpriseId='"+enterpriseId+"' ORDER BY CREATE_TIME DESC ");
        DataTable dataTable = SqlSessionManager.getDataTable(sql);
        if(dataTable!=null&& dataTable.getRowCount()>0){
          for(int i =0;i<dataTable.size();i++){
              DataRow dataRow = dataTable.getRow(i);
              NewLicLog newLicLog = new NewLicLog();
              newLicLog.setEnterpriseId(dataRow.getString("enterpriseId"));
              newLicLog.setAvailbaleDate(dataRow.getString("AVAILABLE_DATE"));
              newLicLog.setEnterpriseName(dataRow.getString("ENTERPRISE_NAME"));
              newLicLog.setAllSignNum(dataRow.getString("ALL_SIGN_NUM"));
              newLicLog.setSubordianteService(dataRow.getString("SUBORDINATE_SERVICE"));
              newLicLog.setKeyMode(dataRow.getString("KEY_MODE"));
              newLicLog.setCreatePdfMode(dataRow.getString("CREATE_PDF_MODE"));
              newLicLog.setCertificateExpiryTime(dataRow.getString("CERTIFICATE_EXPIRY_TIME"));
              newLicLog.setCreateTime(dataRow.getString("CREATE_TIME"));
              list.add(newLicLog);
          }
        }
        return list;
    }

    public boolean deleteLogByEnterpriseId(String enterpriseId){
        Sql sql = new Sql("DELETE FROM g_lic_log WHERE enterpriseId='"+enterpriseId+"'");
        return SqlSessionManager.execute(sql);
    }
}
